package com.yuan.support;

import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v4.content.CursorLoader;

/**
 * create by water.yuan
 * on 2021-07-21
 */
class PictureFileUtils {

    /**
     * 从Intent的uri数据中解析出path
     * 从数据库查找
     * 耗时操作
     */
    public static String getFilePathFromIntent(Intent intent) {
        if (intent == null) {
            return null;
        }

        Uri uri = intent.getData();
        if (uri == null) {
            return null;
        }

        String[] proj = {MediaStore.Images.Media.DATA};
        Cursor cursor = new CursorLoader(ContextUtils.getContextByReflection(), uri, proj,
                null, null, null).loadInBackground();
        cursor.moveToFirst();
        int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        String path = cursor.getString(columnIndex);
        cursor.close();
        return path;
    }

}
